#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 1e6 + 10, INF = 0x3f3f3f3f;

ll n;
string s;
vector<ll> cntl(N), cntr(N), cntm(N);
vector<vector<ll>> g(N);

ll f(ll x)
{
    ll l = 0, r = 0;
    for (ll y : g[x]) {
        if (s[y] == 'C') l += cntl[y];
        if (s[y] == 'P') r += cntr[y];
    }
    r -= cntm[x];
    return l * r;
}

ll dfs(ll x, ll fa)
{
    ll res = s[x] == 'C' ? f(x) : 0;
    for (ll y : g[x]) {
        if (y == fa) continue;
        res += dfs(y, x);
    }
    return res;
}

void solve()
{
    cin >> n >> s; s = " " + s;
    for (ll i = 0;i <= n;i++) {
        g[i].clear();
        cntl[i] = cntr[i] = cntm[i] = 0;
    }
    for (ll i = 1;i < n;i++) {
        ll x, y;cin >> x >> y;
        if (s[x] == 'S' && s[y] == 'C')
            cntl[y]++;
        if (s[x] == 'C' && s[y] == 'S')
            cntl[x]++;
        if (s[x] == 'P' && s[y] == 'C')
            cntr[x]++, cntm[y]++;
        if (s[x] == 'C' && s[y] == 'P')
            cntr[y]++, cntm[x]++;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    cout << dfs(1, 0) << '\n';
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    cin >> T;
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}